package com.example.recycleviewdemo;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Friend> friendList;
    private int[] imgIds = {R.drawable.libai, R.drawable.qingzhao, R.drawable.nongyu, R.drawable.tiger};
    private String[] names;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        names = getResources().getStringArray(R.array.names);
        initFriendList();
        //RecyclerView获取他的对象
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        //LayoutManager用于指定RecyclerView的布局方式
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        //给layoutManager 的展示方式设置为竖直方向
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        FriendAdapter adapter = new FriendAdapter(friendList);
        recyclerView.setAdapter(adapter);
    }

    private void initFriendList() {
        friendList = new ArrayList<Friend>();
        for (int i = 0; i < names.length; i++) {
            friendList.add(new Friend(names[i], imgIds[i]));
        }
    }
}
